using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SceneEnemyPanel : BasePanel
{
    [Header("按钮")]
    public Button closeButton;
    [Header("物品")]
    public Transform parent;
    public GameObject slot;

    //列表
    public List<GameObject> slots = new List<GameObject>();
    public override void OnClose()
    {
        base.OnClose();

        GameManager.Instance.canOprate=true;
    }

    public override void OnInit()
    {
        base.OnInit();
        path="UI/Panel/SceneEnemyPanel";
        type=PanelType.Panel;

        GameManager.Instance.canOprate=false;
    }

    public override void OnShow(params object[] par)
    {
        base.OnShow(par);
        closeButton=item.transform.Find("Close").GetComponent<Button>();
        parent=item.transform.Find("EnemyData/Viewport/Content");
        slot=parent.Find("Slot").gameObject;

        slot.SetActive(false);

        closeButton.onClick.AddListener(CloseButton);
        CreateSlot();
    }
    //创建物品
    public void CreateSlot()
    {
        int index = 0;
        foreach(var i in SceneItemManager.instance.enemies)
        {
            EnemyData data = i.GetComponent<Enemy>().data;

            if(i.activeSelf&&CheckSomeName(i)==false)
            {
                index+=1;
                GameObject newSlot = Instantiate(slot);
                newSlot.SetActive(true);
                newSlot.transform.SetParent(parent);
                if(data.Icon!=null)
                {
                    newSlot.transform.Find("Image").GetComponent<Image>().sprite=data.Icon;
                }
                newSlot.transform.Find("Attack/Num").GetComponent<Text>().text=data.damage.ToString();
                newSlot.transform.Find("Armor/Num").GetComponent<Text>().text=data.armor.ToString();
                newSlot.transform.Find("Hp/Num").GetComponent<Text>().text=data.currHp.ToString();
                newSlot.transform.Find("Coin/Num").GetComponent<Text>().text=data.dropCoin.ToString();
                //加掉落暗影珠，精英怪，Boss标签，对玩家的伤害
                newSlot.transform.Find("Circle/Num").GetComponent<Text>().text=data.dropCircle.ToString();
                newSlot.transform.Find("Name").GetComponent<Text>().text=data.myName;
                string currNums=CalculatePlayerBeHurt(i).ToString();
                if (int.Parse(currNums)>999999) currNums="死亡";
                newSlot.transform.Find("Damage/Num").GetComponent<Text>().text=currNums;
                Enemy enemy = i.GetComponent<Enemy>();
                if (enemy.data.isElite)
                {
                    newSlot.transform.Find("Level2").gameObject.SetActive(true);
                    newSlot.transform.Find("Level3").gameObject.SetActive(false);
                }
                else if (enemy.data.isBoss)
                {
                    newSlot.transform.Find("Level2").gameObject.SetActive(false);
                    newSlot.transform.Find("Level3").gameObject.SetActive(true);
                }
                else
                {
                    newSlot.transform.Find("Level2").gameObject.SetActive(false);
                    newSlot.transform.Find("Level3").gameObject.SetActive(false);
                }
                //选择类型
                ChooseType(newSlot.transform.Find("Type"), enemy.data.type);
                //赋值连击数字
                newSlot.transform.Find("DamageNum/Num").GetComponent<Text>().text = enemy.data.damageNum.ToString();

                slots.Add(newSlot);
            }
        }
        foreach (var i in SceneItemManager.instance.npcs)
        {
            CircleObj currObj = i.GetComponent<CircleObj>();
            if (currObj==null) continue;
            EnemyData data = currObj.enemy.data;

            if (currObj.obj.activeSelf&&CheckSomeName(currObj.enemy.gameObject)==false)
            {
                index+=1;
                GameObject newSlot = Instantiate(slot);
                newSlot.SetActive(true);
                newSlot.transform.SetParent(parent);
                if (data.Icon!=null)
                {
                    newSlot.transform.Find("Image").GetComponent<Image>().sprite=data.Icon;
                }
                newSlot.transform.Find("Attack/Num").GetComponent<Text>().text=data.damage.ToString();
                newSlot.transform.Find("Armor/Num").GetComponent<Text>().text=data.armor.ToString();
                newSlot.transform.Find("Hp/Num").GetComponent<Text>().text=data.currHp.ToString();
                newSlot.transform.Find("Coin/Num").GetComponent<Text>().text=data.dropCoin.ToString();
                newSlot.transform.Find("Circle/Num").GetComponent<Text>().text=data.dropCircle.ToString();
                newSlot.transform.Find("Name").GetComponent<Text>().text=data.myName;
                string currNums = CalculatePlayerBeHurt(currObj.enemy.gameObject).ToString();
                if (int.Parse(currNums)>999999) currNums="死亡";
                newSlot.transform.Find("Damage/Num").GetComponent<Text>().text=currNums;
                Enemy enemy = currObj.enemy;
                if(enemy.data.isElite)
                {
                    newSlot.transform.Find("Level2").gameObject.SetActive(true);
                    newSlot.transform.Find("Level3").gameObject.SetActive(false);
                }
                else if(enemy.data.isBoss)
                {
                    newSlot.transform.Find("Level2").gameObject.SetActive(false);
                    newSlot.transform.Find("Level3").gameObject.SetActive(true);
                }
                else
                {
                    newSlot.transform.Find("Level2").gameObject.SetActive(false);
                    newSlot.transform.Find("Level3").gameObject.SetActive(false);
                }
                //选择类型
                ChooseType(newSlot.transform.Find("Type"), enemy.data.type);
                //赋值连击数字
                newSlot.transform.Find("DamageNum/Num").GetComponent<Text>().text = enemy.data.damageNum.ToString();

                slots.Add(newSlot);
            }
        }
        if(RandomRoomManager.instance!=null&&RandomRoomManager.instance.type==RoomType.Enemy)
        {
            for(int i=0;i<RandomRoomManager.instance.transform.childCount;i++)
            {
                Enemy sc = RandomRoomManager.instance.transform.GetChild(i).GetComponent<Enemy>();

                if(sc!=null&&sc.data.isDie==false)
                {
                    EnemyData data = sc.data;

                    index+=1;
                    GameObject newSlot = Instantiate(slot);
                    newSlot.SetActive(true);
                    newSlot.transform.SetParent(parent);
                    if (data.Icon!=null)
                    {
                        newSlot.transform.Find("Image").GetComponent<Image>().sprite=data.Icon;
                    }
                    newSlot.transform.Find("Attack/Num").GetComponent<Text>().text=data.damage.ToString();
                    newSlot.transform.Find("Armor/Num").GetComponent<Text>().text=data.armor.ToString();
                    newSlot.transform.Find("Hp/Num").GetComponent<Text>().text=data.currHp.ToString();
                    newSlot.transform.Find("Coin/Num").GetComponent<Text>().text=data.dropCoin.ToString();
                    newSlot.transform.Find("Circle/Num").GetComponent<Text>().text=data.dropCircle.ToString();
                    newSlot.transform.Find("Name").GetComponent<Text>().text=data.myName;
                    string currNums = CalculatePlayerBeHurt(sc.gameObject).ToString();
                    if (int.Parse(currNums)>999999) currNums="死亡";
                    newSlot.transform.Find("Damage/Num").GetComponent<Text>().text=currNums;
                    Enemy enemy = sc;
                    if (enemy.data.isElite)
                    {
                        newSlot.transform.Find("Level2").gameObject.SetActive(true);
                        newSlot.transform.Find("Level3").gameObject.SetActive(false);
                    }
                    else if (enemy.data.isBoss)
                    {
                        newSlot.transform.Find("Level2").gameObject.SetActive(false);
                        newSlot.transform.Find("Level3").gameObject.SetActive(true);
                    }
                    else
                    {
                        newSlot.transform.Find("Level2").gameObject.SetActive(false);
                        newSlot.transform.Find("Level3").gameObject.SetActive(false);
                    }
                    //选择类型
                    ChooseType(newSlot.transform.Find("Type"), enemy.data.type);
                    //赋值连击数字
                    newSlot.transform.Find("DamageNum/Num").GetComponent<Text>().text = enemy.data.damageNum.ToString();

                    slots.Add(newSlot);
                }
            }
        }

        GridLayoutGroup layer = parent.GetComponent<GridLayoutGroup>();
        Vector2 vec2 = parent.GetComponent<RectTransform>().sizeDelta;
        parent.GetComponent<RectTransform>().sizeDelta=new Vector2(vec2.x,index*(layer.cellSize.y+layer.spacing.y)+layer.padding.top-layer.spacing.y);

        //适配机型
        foreach(var i in slots)
        {
            OnAdaptiveResolution(i);
        }
    }
    //适配分辨率
    public void OnAdaptiveResolution(GameObject obj)
    {
        ////获取机型
        //string deviceModel = SystemInfo.deviceModel;
        ////机型检查适配
        //if(deviceModel.Contains("Mi A3")||deviceModel.Contains("xiaomi Mi A3")||
        //    deviceModel.Contains("Redmi 4")||deviceModel.Contains("xiaomi Redm 4"))
        //{
        //    obj.transform.localScale=new Vector3(1, 1, 1);
        //    Debug.Log("xxxx");
        //}
        //else
        //{
        //    obj.transform.localScale=new Vector3(1, 1, 1);
        //}
        
        //强制适配为1
        obj.transform.localScale=new Vector3(1, 1, 1);

    }
    //关闭
    public void CloseButton()
    {
        Close();

        AudManager.instance.PlayUIAud(AudManager.instance.uiDic["点击"], 1);
    }
    //计算对玩家的伤害
    public int CalculatePlayerBeHurt(GameObject obj)
    {
        Player player = GameManager.Instance.player;
        Enemy enemy = obj.GetComponent<Enemy>();
        //攻击力防御力获取
        //计算玩家此时的攻击力，防御力
        int playerDamage = player.CalculateCurrFinalDamage(player.damage, enemy);
        int playerArmor = player.CalculateCurrFinalArmor(player.armor, enemy);
        //计算敌人的防御，攻击
        int enemyDamage = enemy.data.damage;
        int enemyArmor = enemy.data.armor;
        //计算需要攻击几次
        //玩家敌人血量/玩家攻击
        int num = enemy.data.currHp/playerDamage;
        Debug.Log("原始：" + num);
        if (num < 1)
        {
            num = 0;
        }
        else
        {
            num = Mathf.CeilToInt(num);
            num = num <= 1 ? 1 : num;
        }
        Debug.Log("此刻：" + num);
        //如果佩戴魔戒
        if (ArtifactManager.instance.SeekArtifact("魔戒"))
        {
            num-=1;
        }
        //一旦次数小于等于1，直接返回0
        //if (num<=1)
        //{
        //    return 0;
        //}
        //计算敌人增伤
        Debug.Log("玩家防御：" + playerArmor + " / 敌人伤害：" + enemyDamage);
        //计算攻击大于还是小于
        if (enemyDamage>playerArmor)
        {
            float deAmount = (enemyDamage - playerArmor) / (playerArmor*1.0f);
            deAmount = deAmount > 0.5f ? 0.5f : deAmount;
            enemyDamage = enemyDamage + Mathf.RoundToInt(deAmount * enemyDamage);
        }
        else
        {
            float deAmount = (playerArmor-enemyDamage) / (playerArmor*1.0f);
            deAmount = deAmount > 0.9f ? 0.9f : deAmount;
            enemyDamage = enemyDamage - Mathf.RoundToInt(deAmount * enemyDamage);
            enemyDamage=enemyDamage<=0 ? 1 : enemyDamage;
        }
        
        //查看是否佩戴了速刃
        if(ArtifactManager.instance.SeekArtifact("速刃")==false)
        {
            if(enemy.data.type==EnemyType.Quick)
            {
                num+=1;
            }
            else if(enemy.data.type==EnemyType.Routine)
            {
                if(player.damage<enemy.data.damage)
                {
                    num+=1;
                }
            }
        }

        Debug.Log("次数：" + num + " / 敌人伤害：" + enemyDamage+"频率："+enemy.data.damageNum);

        return num * enemyDamage*enemy.data.damageNum;
    }
    //查看是不是有同名称的装入了列表
    public bool CheckSomeName(GameObject obj)
    {
        foreach(var i in slots)
        {
            Enemy enemy1 = obj.GetComponent<Enemy>();

            if (enemy1.data.myName==i.transform.Find("Name").GetComponent<Text>().text)
            {
                return true;
            }
        }

        return false;
    }
    //选择类型
    public void ChooseType(Transform trans,EnemyType type)
    {
        GameObject slow = trans.Find("Slow").gameObject;
        GameObject rountine = trans.Find("Rountine").gameObject;
        GameObject quick = trans.Find("Quick").gameObject;

        slow.SetActive(false);
        rountine.SetActive(false);
        quick.SetActive(false);

        if (type==EnemyType.Slow)
        {
            slow.SetActive(true);
        }
        else if(type==EnemyType.Routine)
        {
            rountine.SetActive(true);
        }
        else if (type==EnemyType.Quick)
        {
            quick.SetActive(true);
        }
    }
}
